import 'package:flutter_deer/database/database_manager.dart';
import 'package:flutter_deer/dl/dl.dart';
import 'package:flutter_deer/generated/json/base/json_field.dart';
import 'package:flutter_deer/generated/json/get_all_goods_list.g.dart';
import 'package:flutter_deer/objectbox.g.dart';
import 'package:get/get.dart';

// part 'get_all_goods_list.g.dart'; // 生成的文件


@Entity()
@JsonSerializable()
class GetAllGoodsList {
  @Id() // 自增主键
  int id = 0;

  String goodsName = '';
  String unitName = '';
  String quantity = '';
  double price = 0.0;
  int goodAmount = 0;
  int virtual = 0;
  String coverImageUrl = '';
  String goodsBarCode = '';
  String goodsCategoryName = '';
  String goodsCategoryId = '';
  String goodsId = '';
  String salesChannelGoodsId = '';
  String status = '';
  String statusName = '';
  String residueStock = '';
  String freezeStock = '';

  @Transient()
  @JSONField(deserialize: false, serialize: false)
  Rx<int> count = 1.obs;

  //计算总金额
  double getTotalPrice() {
    return price * 100 * count.value;
  }

  GetAllGoodsList();

  factory GetAllGoodsList.fromJson(Map<String, dynamic> json) => $GetAllGoodsListFromJson(json);

  Map<String, dynamic> toJson() => $GetAllGoodsListToJson(this);

  /// 删除所有
  static Future<void> deleteAll() async {
    final store = getIt<ObjectIdBox>().store;
    final box = store.box<GetAllGoodsList>();
    box.removeAll();
  }

  /// 保存到数据库
  static Future<void> saveAll(List<GetAllGoodsList> list) async {
    // 保存到数据库
    final store = getIt<ObjectIdBox>().store;
    final box = store.box<GetAllGoodsList>();
    box.putMany(list);
  }

  /// 从数据库读取
  static Future<List<GetAllGoodsList>?> getAllGoods() async {
    final store = getIt<ObjectIdBox>().store;
    final box = store.box<GetAllGoodsList>();
    return box.getAll();
  }

  /// 根据商品分类ID查询商品列表
  static Future<List<GetAllGoodsList>?> getGoodsByCategoryId(String categoryId) async {
    final store = getIt<ObjectIdBox>().store;
    final box = store.box<GetAllGoodsList>();
    return box.query(GetAllGoodsList_.goodsCategoryId.equals(categoryId)).build().find();
  }
}
